package thread;

import java.util.Date;

public class Thread1 {
	public static void main(String[] args) throws InterruptedException {
//		test();
//		test1();
//		test2();
//		test3();
//		daemonTest();
//		joinTest();
		activeThread();
	}
	private static void activeThread() {
		Thread t = new Thread();
		t.start();
		Thread t1 = new Thread();
		t1.start();
		Thread t2 = new Thread();
		t2.start();
		Thread t3 = new Thread();
		t3.start();
		Thread t4 = new Thread();
		t4.start();
		System.out.println(Thread.activeCount());
	}
	//一个线程中用join调用另一个线程，将另一个线程未执行完的代码执行完再继续执行本线程
	//可能使用到被调用的某个计算结果
	private static void joinTest() throws InterruptedException {
		final Thread t = new Thread(){//局部内部类使用外部的局部变量加final
			@Override
			public void run() {
				for (int i = 0; i < 1000; i++) {
					System.out.println("t thread:"+i);
				}
			}
		};
		class T extends Thread{
			@Override
			public void run() {
				for (int i = 0; i < 1000; i++) {
					System.out.println(Thread.currentThread().getName()+","+i);
					if(i==500){
						try {
							t.join();//t1调用 t.join，执行到500时将t线程未执行完的代码执行完再继续执行
						} catch (InterruptedException e) {
							e.printStackTrace();
						}//局部内部类使用外部的局部变量加final
					}
				}
			}
		}
		Thread t1 = new T();
		t1.start();
		t.start();
	}

	private static void daemonTest() throws InterruptedException {
		class T extends Thread{
			int times;
			public T (int times){
				this.times=times;
			}
			@Override
			public void run() {
				for (int i = 0; i < times; i++) {
					System.out.println(Thread.currentThread().getName()+","+i);
				}
			}
		}
		Thread t1 = new T(100);
		Thread t2 = new T(1000);
		t2.setDaemon(true);
		t1.start();
		t2.start();
		t2.join();//主线程调用t2.join()，将t2未执行完的代码由主线程执行
	}

	private static void test3() {
		class T extends Thread{
			boolean flag;
			public T(boolean flag){
				this.flag=flag;
			}
			public void run(){
				for (int i = 0; i < 100; i++) {
					System.out.println(Thread.currentThread().getName()+","+i);
					if(flag)Thread.yield();
				}
			}
		}
		Thread t1 = new T(true);
		t1.setName("t1");
		System.out.println(t1.getId());
		t1.setPriority(10);
		System.out.println(t1.getPriority());
		Thread t2= new T(false);
		t2.setPriority(1);
		t2.setName("t2");
		System.out.println(t2.getId());
		t1.start();
		System.out.println(t1.getContextClassLoader());
		t2.start();
	}

	private static void test2() {
		class T1 extends Thread{
			public void run(){//重写方法不能比父类声明抛的异常更多
				Thread t = Thread.currentThread();
				for (int i = 0; i < 10; i++) {
					String name = t.getName();
					System.out.println(name);
					String s = "今天是个好日子好日子";
					System.out.println(s.charAt(i));
					System.out.println(new Date());
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}
		Thread t1 = new T1();
		Thread t2 = new T1();
		t1.setName("mazi");
		t2.setName("neihao");
		t1.start();
		t1.yield();
		t2.start();
	}

	private static void test1() {
		class R implements Runnable{

			public void run() {
				for(int i=0;i<100;i++){
					System.out.println("i="+i);
					killTime();
				}
			}
			
		}
		R r = new R();
		Thread t1 = new Thread(r);
		R r1 = new R();
		Thread t2 = new Thread(r1);
		
		t1.start();
		System.out.println("第一个线程启动");
		t1.yield();
		t2.start();
		System.out.println("第二个线程启动");
	}

	private static void test() {
		class T1 extends Thread{
			@Override
			public void run() {
				for (int i = 0; i < 100; i++) {
//					System.out.println("i="+i);
					killTime();
				}
			}
		}
		Thread t1 = new T1();
		Thread t2 = new T1();
		t1.start();
		System.out.println("第一个线程启动");
		System.out.println(Thread.currentThread());
		t2.start();
		System.out.println("第二个线程启动");
		System.out.println(Thread.activeCount());
	}
	static void killTime(){
		String s ="s;lkdjfas;ldfkj";
		String str = null;
		for (int i = 0; i < 100; i++) {
			str+=s;
		}
	}
}
